<%@page import="com.mconnect.msurvey.model.ResponderConfirm"%>
<%@page import="com.mconnect.msurvey.controller.ConfirmManager"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.mconnect.msurvey.model.Survey"%>
<%@page import="java.util.List"%>
<%@page import="com.mconnect.msurvey.controller.SurveyManager"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
	 <style>
		body { font-size: 62.5%; }
		label, input { display:block; }
		input.text { margin-bottom:12px; width:95%; padding: .4em; }
		fieldset { padding:0; border:0; margin-top:25px; }
		h1 { font-size: 1.2em; margin: .6em 0; }
		div#users-contain { width: 350px; margin: 20px 0; }
		div#users-contain table { margin: 1em 0; border-collapse: collapse; width: 100%; }
		div#users-contain table td, div#users-contain table th { border: 1px solid #eee; padding: .6em 10px; text-align: left; }
		.ui-dialog .ui-state-error { padding: .3em; }
		.validateTips { border: 1px solid transparent; padding: 0.3em; }
	</style>
</head>
	<%String id_plan = request.getParameter("id_plan");
	int id = Integer.parseInt(id_plan);
	String firstname = request.getParameter("firstname");
	String lastname = request.getParameter("lastname");
	String phone = request.getParameter("phone");
	String pesel = request.getParameter("pesel");
	String status = request.getParameter("status");
	String pageN = request.getParameter("pageNumber");
	String statusPesel = request.getParameter("statusPesel");
	String dateFrom = request.getParameter("dateFrom");
	String dateTo = request.getParameter("dateTo");
	String timeFrom = request.getParameter("timeFrom");
	String timeTo = request.getParameter("timeTo");
	int pageNumber = 1;
	if(pageN != null){
		pageNumber = Integer.parseInt(pageN);
	}
	SurveyManager sM = new SurveyManager();
	List<Survey> surveyList = new ArrayList<Survey>();
	surveyList = sM.getConfirmSurveyList(id, pageNumber, firstname, lastname, phone, pesel, status, statusPesel, dateFrom, dateTo, timeFrom, timeTo);
	
	ConfirmManager cM = new ConfirmManager();
	ResponderConfirm confirm = null;
	%>
	<script type="text/javascript">
					$(function() {
						var plan =  $( "#id_plan" ).val();
						var firstname = $( "#p_firstname" ).val();
						var lastname = $( "#p_lastname" ).val();
						var pesel = $( "#p_pesel" ).val();
						var status = $( "#p_status" ).val();
						var phone = $( "#p_phone" ).val();
						var statusPesel = $( "#s_statusPesel" ).val();
						var dateFrom = $( "#datepicker1" ).val();
						var dateTo = $( "#datepicker2" ).val();
						var timeFrom = $( "#timeFrom" ).val();
						var timeTo = $( "#timeTo" ).val();
							
						$('#nextPage').click(function(){
							var startPage = parseFloat($( "#next" ).val());
							var page = startPage + 1;
								
							$.get(
								'confirm/confirmListContent.jsp',
								{
									pageNumber: page, id_plan: plan, firstname: firstname, lastname : lastname, phone : phone,
									pesel : pesel, status : status, statusPesel : statusPesel, dateFrom : dateFrom, dateTo : dateTo, timeFrom : timeFrom, timeTo : timeTo 
												
								},
								function (content) {
									$('#users-contain').html(content);
									$( "#next" ).val(page);
									return false;
								}
							);
								
							});
						
						$('#previousPage').click(function(){
							var startPage = parseFloat($( "#next" ).val());
							var page = startPage - 1;
							
							if(page >= 1){
								$.get(
									'confirm/confirmListContent.jsp',
									{
										pageNumber: page, id_plan: plan, firstname: firstname, lastname : lastname, phone : phone,
										pesel : pesel, status : status, statusPesel : statusPesel, dateFrom : dateFrom, dateTo : dateTo, timeFrom : timeFrom, timeTo : timeTo 
									},
									function (content) {
										$('#users-contain').html(content);
										$( "#next" ).val(page);
										return false;
									}
								);
								
							}	
						});
						
						$('.surveyContent_dialogLink').click(function(){
							var _id_confirm = $(this).prev('input').val();
							var _id_responder = $(this).prev('input').prev('input').val();
							var _firstname = $(this).prev('input').prev('input').prev('input').val();
							var _lastname = $(this).prev('input').prev('input').prev('input').prev('input').val();
							var _phone = $(this).prev('input').prev('input').prev('input').prev('input').prev('input').val();
							var _mail = $(this).prev('input').prev('input').prev('input').prev('input').prev('input').prev('input').val();
							var confirmUser =$(this).prev('input').prev('input').prev('input').prev('input').prev('input').prev('input').prev('input').val();
							var _time = $(this).prev('input').prev('input').prev('input').prev('input').prev('input').prev('input').prev('input').prev('input').val();
							var _date = $(this).prev('input').prev('input').prev('input').prev('input').prev('input').prev('input').prev('input').prev('input').prev('input').val();
							var _confDate = $(this).prev('input').prev('input').prev('input').prev('input').prev('input').prev('input').prev('input').prev('input').prev('input').prev('input').val();
								$.post(
										'confirm/confirmListDetails.jsp',
										{id_confirm : _id_confirm, id_responder: _id_responder, firstname : _firstname, lastname: _lastname, phone: _phone, mail: _mail, date : _date,
										 time: _time, confirmUser : confirmUser, confirmDate : _confDate 
										 },
										function (data){
											$('body').append(data);
										}
									);
							
							return false;
						});
						
					});
				</script>
						<div id="users-contain" class="ui-widget" style=" width: 850px; text-align: center;">
						<% if(surveyList.isEmpty() && pageNumber == 1){ %>
							<div class="ui-widget" style="width: 70%;  margin-top: 30px; margin-left: 15%">
								<div class="ui-state-error ui-corner-all" style="padding: 0 .7em;">
									<p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span>
									<strong>Nie znaleziono wyników spełniających twoje kryteria. </strong> <br /> Zmień kryteria wyszukiwań.</p>
								</div>
							</div>
						<%}else{ %>
							<input type="hidden" id="id_plan" value="<%=id_plan %>">
							<input type="hidden" id="p_firstname" value="<%=firstname %>">
							<input type="hidden" id="p_lastname" value="<%=lastname %>">
							<input type="hidden" id="p_phone" value="<%=phone %>">
							<input type="hidden" id="p_pesel" value="<%=pesel %>">
							<input type="hidden" id="p_status" value="<%=status %>">
							<input type="hidden" id="p_statusPesel" value="<%=statusPesel %>">
							<input type="hidden" id="dateFrom" value="<%=dateFrom %>">
							<input type="hidden" id="dateTo" value="<%=dateTo %>">
							<input type="hidden" id="timeFrom" value="<%=timeFrom %>">
							<input type="hidden" id="timeTo" value="<%=timeTo %>">
							<table id="users" class="ui-widget ui-widget-content" style="width: 850px;">
							<thead>
							<tr class="ui-widget-header ">
								<th width="140px;">Imie</th>
								<th width="140px;">Nazwisko</th>
								<th width="140px;">Telefon</th>
								<th width="140px;">Data spotkania</th>
								<th width="140px;">Godzina spotkania</th>
								<th width="140px;">Szczegóły</th>
							</tr>
							<tbody>
								<%
									for(Survey survey : surveyList){
									confirm = cM.getConfirmByIdResponder(survey.getId_responder().getId_responder());
								%>
									<tr>
										<td><%=survey.getId_responder().getFirstname() %></td>
										<td><%=survey.getId_responder().getLastname() %></td>
										<td><%=survey.getId_responder().getPhone() %></td>
										<td><%=confirm.getArrivalDate() %></td>
										<td><%=confirm.getArrivalTime() %></td>
										<td>
											<input type="hidden" value="<%=confirm.getConfirmDate() %>" /><input type="hidden" value="<%=confirm.getArrivalDate() %>" />
											<input type="hidden" value="<%=confirm.getArrivalTime() %>" /><input type="hidden" value="<%=confirm.getConfirm_id_user() %>" />
											<input type="hidden" value="<%=survey.getId_responder().getMail() %>" /><input type="hidden" value="<%=survey.getId_responder().getPhone() %>" />
											<input type="hidden" value="<%=survey.getId_responder().getLastname() %>" /><input type="hidden" value="<%=survey.getId_responder().getFirstname() %>" />
											<input type="hidden" value="<%=survey.getId_responder().getId_responder() %>" /><input type="hidden" value="<%=confirm.getId_confirm() %>" />
											<a class="surveyContent_dialogLink" href="#">szczegóły</a>
										</td>
									</tr>
								<%} %>
								<tr>
									<td colspan="6" id="form">
										<div>
												<div style="float: left; margin-top: 3px;"><a href="#" id="previousPage" style="display: block;"><img alt="Previous" src="resources/images/previous.png"> </a></div>
												<div style="float: left; margin-left: 5px; "><input type="text" id="next" style="width: 20px; text-align: center;"  value="1" ></div>
												<div style="float: left; margin-left: 5px; margin-top: 3px;"><a href="#" id="nextPage" style="display: block;"><img alt="Next" src="resources/images/next.png"> </a> </div>
										</div>
									 </td>
								</tr>
							</tbody>	
							</table>
							<%} %>
					</div>
</html>